/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int dfs(TreeNode* root, int& k)
    {
        if (root == nullptr)return 0;
        int Left = dfs(root->left, k);
        if (Left > 0)return Left;
        cout << Left << endl;
        k--;
        if (k == 0) return root->val;
        int Right = dfs(root->right, k);
        //if(right>0)return right;

        return Left > 0 ? Left : Right;

    }

    int kthSmallest(TreeNode* root, int k)
    {
        return dfs(root, k);

    }

};